package com.tyqx.hashchainbackend.controller;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbackend.service.SysResourceService;
import com.tyqx.hashchainbackend.service.SysRoleResourceService;
import com.tyqx.hashchainbean.module.SysResource;
import com.tyqx.hashchainbean.module.SysRoleResource;
import com.tyqx.hashchainbean.vo.*;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.*;

@Controller
@RequestMapping("/sysResource")
public class SysResourceController {

    @Resource
    SysResourceService sysResourceService;

    @Resource
    SysRoleResourceService sysRoleResourceService;

    @RequestMapping("/queryResource")
    @ResponseBody
    public String queryPermission(@RequestParam(value = "roleId") Long roleId) {
        try {
            List<PerOrResourceResultVO> resources = sysResourceService.queryAllResource();
            List<PerOrResourceParamVO> sysRoleResources = sysRoleResourceService.queryRoleResource(roleId);
            List<PerOrResourceParamVO> sysRolePers = new ArrayList<>();
            for (PerOrResourceParamVO vo : sysRoleResources) {
                List<Long> perIds = sysResourceService.queryResId(vo.getPerOrRrId());
                if (perIds.isEmpty()) {
                    sysRolePers.add(vo);
                }
            }
            //调用方法实现角色树
//            return TreeNode.createComboTreeTree(resources, sysRolePers, 0l));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //分配ziyuan
    @RequestMapping(value = "/sysRoleResourceSaveyBRoleId", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto sysRoleResourceSaveyBRoleId(@RequestParam("id") String id,@RequestParam("roleId") Long roleId) throws Exception {
        /*if("".equals(id)){
            sysRoleResourceService.removeByRoleId(roleId);
            return DtoUtil.returnSuccess("success");
        }*/
        String[] IDs = id.split(",");
        //之前已经拥有的角色
        List<SysResourceIDVO> sysResourceIDVOList = sysRoleResourceService.getSysRoleResourceByRoleId(roleId);
        //已经拥有的角色+先赋予的角色
        List<Integer> resIdList=new ArrayList<>();
        if(IDs.length>0){
            for (int b=0;b< IDs.length;b++){
                resIdList.add(Integer.valueOf(IDs[b]));
            }
        }else{
            sysRoleResourceService.removeByRoleId(roleId);
            return DtoUtil.returnSuccess("success");
        }

        for (int i = 0; i <sysResourceIDVOList.size(); i++) {
            for (int j = 0; j < resIdList.size(); j++) {
                if (resIdList.get(j) == sysResourceIDVOList.get(i).getResId()) {
                    resIdList.remove(j);
                }
            }
        }


        //sysResourceIDVOList.remove(sysResourceIDVOList1);

        int count = 0;
        String message = "";
        /*if(sysResourceIDVOList1.size()>0){
            for (int i = 0; i <sysResourceIDVOList1.size(); i++) {
                count += sysRoleResourceService.removeByResId(Long.valueOf(sysResourceIDVOList1.get(i).getResId()));
            }
        }*/

        if(resIdList.size()>0) {
            List<SysRoleResourceVO> sysRoleResourceList=new ArrayList<>();
            for (int k = 0; k < resIdList.size(); k++) {
                SysRoleResourceVO sysRoleResource=new SysRoleResourceVO();
                sysRoleResource.setRrResId(Long.valueOf(resIdList.get(k)));
                sysRoleResource.setRrRoleId(roleId);
                sysRoleResource.setRrCreationDate(new Date());
                sysRoleResourceList.add(sysRoleResource);
            }
            count+=sysRoleResourceService.save(sysRoleResourceList);
        }
        if (count > 0) {
            message = "success";
            return DtoUtil.returnSuccess(message);
        }else {
            message="error";
            return DtoUtil.returnFail(message,"100001");
        }

    }

    /**
     * 删除角色已拥有的资源
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/removePerOrRr")
    @ResponseBody
    public Dto removePerOrRr(@RequestParam("roleId") Long roleId, @RequestParam("rrId") Long rrId) {
        try {
            List<Long> longs = sysResourceService.queryResId(rrId);
            longs.add(rrId);
            if (sysRoleResourceService.removeById(roleId,longs)) {
                return DtoUtil.returnSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("删除失败", "100000");
    }

    /**
     * 查询用户已分配的资源
     *
     * @param roleId
     * @param rrId
     * @return
     */
    @RequestMapping("/queryPerOrRr")
    @ResponseBody
    public Dto queryPerOrRr(@RequestParam("roleId") Long roleId, @RequestParam("rrId") Long rrId) {
        List<PerOrResourceParamVO> sysRoleResources = null;
        try {
            sysRoleResources = sysRoleResourceService.queryRoleResource(roleId);
            for (PerOrResourceParamVO vo : sysRoleResources) {
                if (vo.getPerOrRrId() == rrId) {
                    return DtoUtil.returnSuccess();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("", "100000");
    }

    @RequestMapping(value = "/queryResourceByRoleId", method = RequestMethod.GET)
    @ResponseBody
    public List<SysResourceVO> queryResourceByRoleId(@RequestParam(value = "roleId") Long roleId){
        return sysResourceService.getAllByRoleId(roleId);
    }

    /**
     * 查询用户已分配的资源
     *
     * @param userId
     * @param resId
     * @return
     */
    @RequestMapping("/queryResourceTree")
    @ResponseBody
    public List<SysResource> queryResourceTree(@RequestParam("userId") Long userId, @RequestParam("resId") Long resId) {
        Map<String, Object> param =new HashMap<>();
        param.put("userId",userId);
        param.put("resId",resId);
        try {
            List<SysResource> sysResources= sysResourceService.queryTree(param);
           return sysResources;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
